/**
 * Author : wang885298.
 * Date   : 2018/6/20.
 * File   : fileServer.文件服务：文件的上传
 */
import axios from 'axios'

/**
 * 上传文件
 * @param fileName 文件名
 * @param file 文件对象
 * @returns {*}
 */
async function upload ({fileName, file}) {
  console.log('2/**', file)
  return new Promise((resolve, reject) => {
    let formData = new window.FormData()
    formData.append('file', file)
    axios({
      method: 'post',
      url: 'http://192.168.254.58:8888/api/addSnapShot',
      responseType: 'json',
      data: formData
    }).then(response => {
      if (/ERR_/.test(response.data)) {
        let errorJson = JSON.parse(response.data.substr(4))
        reject(errorJson)
      } else {
        resolve(response.data)
      }
    }).catch(e => {
      console.error('上传文件请求失败', e)
      reject(e)
    })
  })
}
export { upload }
